<?php

namespace App\Http\Controllers\Admin;



use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;
use \App\Supplier;
use DB;
class SupplierController extends Controller
{
  //商品列表
    public function goods_lists(){
    	$req=request();
    	$list= DB::table('jtkj_supplier_goods')->paginate(5);
      return view('Admin/supplier/goods_lists',['msg'=>$list]);
    }
  //商品添加
    public function goods_add(){

    	$list= DB::table('jtkj_goods_types')->get();
    	$lists= DB::table('jtkj_suppliers')->get();
    	return view('Admin/supplier/goods_add',['msg'=>$list],['ms'=>$lists]);

    }
//商品添加
    public function addPost(){

      $req=request();
      $file =$req->file('pic');
      $destinationPath='public/img/';
      $extension=$file->getClientOriginalExtension();
      $fileName=str_random(5).".".$extension;
      $file->move($destinationPath,$fileName);
      $filePath=asset($destinationPath.$fileName);
        $re=DB::table('jtkj_supplier_goods')->insert(['goods_name'=>$req->name,'category_id'=>$req->cat,'goods_price'=>$req->price,'goods_img'=>$filePath,'g_id'=>$req->dian]);
        if($re){
         return redirect()->action('Admin\SupplierController@goods_lists');
        }else{
          echo "失败";
        }
    }
//商品删除
    public function del($id){

      $del= DB::table('jtkj_supplier_goods')->where('goods_id',$id)->delete();
        if($del){
          return redirect('admin/goods_lists');
        }else{
          return "删除失败";
        }
    }
    //商品修改
    public function goods_up($id){
        $req=request();
        if(empty($_POST)){
          $row=  DB::table('jtkj_supplier_goods')->where('goods_id',$id)->first();
          $list= DB::table('jtkj_goods_types')->get();
          return view('Admin/supplier/goods_up',['row'=>$row],['msg'=>$list]);
        }else{

          $file =$req->file('pic');
          $destinationPath='public/img/';
          $extension=$file->getClientOriginalExtension();
          $fileName=str_random(5).".".$extension;
          $file->move($destinationPath,$fileName);
          $filePath=asset($destinationPath.$fileName);
            $up=DB::table('jtkj_supplier_goods')->where('goods_id',$id)->update(['goods_name'=>$_POST['name'],'category_id'=>$_POST['cat'],'goods_price'=>$_POST['price'],'goods_img'=>$filePath]);
            if($up){
                return redirect('admin/goods_lists');
            }
           
        }
    }
//店铺列表
    public function dian(){

    	$req=request();
    	$list= DB::table('jtkj_suppliers')->paginate(5);
      return view('Admin/supplier/dian',['msg'=>$list]);
    }
//添加店铺
    public function dianAdd(){
		  $list= DB::table('jtkj_goods_types')->get();
    	return view('Admin/supplier/dianAdd',['msg'=>$list]);
    }
    //店铺删除
    public function dian_del($id){

      $del= DB::table('jtkj_suppliers')->where('g_id',$id)->delete();
        if($del){

        return redirect('admin/dian');
        }else{
         return "删除失败";
        }
    }
//店铺修改
    public function dianUp($id){
        $req=request();
      
        if(empty($_POST)){
          $row=  DB::table('jtkj_suppliers')->where('g_id',$id)->first();
          $list= DB::table('jtkj_goods_types')->get();
          return view('Admin/supplier/dianUp',['row'=>$row],['msg'=>$list]);
        }else{

          $file =$req->file('pic');
          $destinationPath='public/img/';
          $extension=$file->getClientOriginalExtension();
          $fileName=str_random(5).".".$extension;
          $file->move($destinationPath,$fileName);
          $filePath=asset($destinationPath.$fileName);
            $up=DB::table('jtkj_suppliers')->where('g_id',$id)->update(['g_name'=>$req->name,'g_tel'=>$req->tel,'g_from'=>$req->dizhi,'g_logo'=>$filePath,'g_qu'=>$req->qu,'category_id'=>$req->cat,'g_user'=>$req->user,'g_jyfw'=>$req->jyfw]);
            if($up){
                return redirect('admin/dian');
            }
         }
    }

//添加店铺
    public function dianAddPost(){
      $req=request();
      $file =$req->file('pic');
      $destinationPath='public/img/';
      $extension=$file->getClientOriginalExtension();
      $fileName=str_random(5).".".$extension;
      $file->move($destinationPath,$fileName);
      $filePath=asset($destinationPath.$fileName);
        $re=DB::table('jtkj_suppliers')->insert(['g_name'=>$req->name,'g_tel'=>$req->tel,'g_from'=>$req->dizhi,'g_logo'=>$filePath,'g_qu'=>$req->qu,'category_id'=>$req->cat,'g_user'=>$req->user,'g_jyfw'=>$req->jyfw]);
        if($re){
         return redirect()->action('Admin\SupplierController@dian');
        }else{
          echo "失败";
        }

    }
//优惠活动添加
    public function activeAdd($id){
      
        $row=  DB::table('jtkj_supplier_goods')->where('goods_id',$id)->first();
        return view('Admin/supplier/activeAdd',['msg'=>$row]);
    }
    //优惠活动添加
    public function activeAddPost(){

      $req=request();
      $file =$req->file('pic');
      $destinationPath='public/img/';
      $extension=$file->getClientOriginalExtension();
      $fileName=str_random(5).".".$extension;
      $file->move($destinationPath,$fileName);
      $filePath=asset($destinationPath.$fileName);
        $re=DB::table('jtkj_favourable')->insert(['goods_hd_title'=>$req->title,'goods_hd_content'=>$req->content,'goods_hd_img'=>$filePath,'goods_hd_start_time'=>$req->start_time,'goods_id'=>$req->id,'dian_id'=>$req->dian_id]);
        if($re){
         return redirect()->action('Admin\SupplierController@active_lists');
        }else{
          echo "失败";
        }
    }
//优惠活动列表
    public function active_lists(){

      $row=  DB::table('jtkj_favourable')->paginate(5);
      return view('Admin/supplier/active_lists',['msg'=>$row]);
    }
//删除优惠活动
    public function activeDel($id){
      $del= DB::table('jtkj_favourable')->where('goods_hd_id',$id)->delete();
        if($del){
          return redirect('admin/active_lists');
        }else{
         return "删除失败";
        }
    }
//修改优惠活动
    public function activeUp($id){

      $req=request();
      
        if(empty($_POST)){
          $row=  DB::table('jtkj_favourable')->where('goods_hd_id',$id)->first();
            return view('Admin/supplier/activeUp',['row'=>$row]);
        }else{

           $file =$req->file('pic');
          $destinationPath='public/img/';
          $extension=$file->getClientOriginalExtension();
          $fileName=str_random(5).".".$extension;
          $file->move($destinationPath,$fileName);
          $filePath=asset($destinationPath.$fileName);
            $up=DB::table('jtkj_favourable')->where('goods_hd_id',$id)->update(['goods_hd_title'=>$req->title,'goods_hd_content'=>$req->content,'goods_hd_img'=>$filePath,'goods_hd_start_time'=>$req->start_time,'goods_id'=>$req->id,'dian_id'=>$req->dian_id]);
            if($up){
                return redirect('admin/active_lists');
            }
          
        }
  }

//门店相册展示
  public function supplier_picture(){

      $req=request();
      $list= DB::table('jtkj_img')->paginate(5);
      foreach($list as $key=>$val){
        $row[$key]=(array)$val;
      }
     $msg = DB::table('jtkj_img')
            ->join('jtkj_suppliers', 'jtkj_img.g_id','=', 'jtkj_suppliers.g_id')
            ->select('jtkj_img.*','jtkj_suppliers.g_name')
            ->paginate(5);
        return view('Admin/supplier/supplier_picture',['msg'=>$msg]);
  }
//门店相册添加
  public function supplier_pictureAdd(){

    $row=  DB::table('jtkj_suppliers')->get();
    return view('Admin/supplier/supplier_pictureAdd',['row'=>$row]);
   }
   //门店相册添加
   public function supplier_picturePost(){

    $req=request();
      $file =$req->file('pic');
      $destinationPath='public/img/';
      $extension=$file->getClientOriginalExtension();
      $fileName=str_random(5).".".$extension;
      $file->move($destinationPath,$fileName);
      $filePath=asset($destinationPath.$fileName);
        $re=DB::table('jtkj_img')->insert(['img_url'=>$filePath,'g_id'=>$req->g_id]);
        if($re){
         return redirect()->action('Admin\SupplierController@supplier_picture');

        }else{
          echo "失败";
        }

   }
   //门店相册删除
   public function supplier_pictureDel($id){

      $del= DB::table('jtkj_img')->where('img_id',$id)->delete();
        if($del){
          return redirect('admin/supplier_picture');
        }else{
         return "删除失败";
        }

   }
//商品属性
   public function shu(){
      $req=request();
      $msg = DB::table('jtkj_goods_shus')
            ->join('jtkj_supplier_goods', 'jtkj_goods_shus.goods_id','=', 'jtkj_supplier_goods.goods_id')
            ->select('jtkj_goods_shus.*','jtkj_supplier_goods.goods_name')
            ->paginate(5);
      return view('Admin/supplier/shu',['msg'=>$msg]);

   }
   public function shuAdd($id){

        $msgs=  DB::table('jtkj_goods_shus')->where('goods_id',$id)->get();
        foreach($row as $key=>$val){
            $msg[$key]=(array)$val;
        }
        var_dump($msg[$key]);
        if($msg=="")
        {
          $row=  DB::table('jtkj_supplier_goods')->where('goods_id',$id)->get();
        }else{
          $del= DB::table('jtkj_goods_shus')->where('goods_id',$id)->delete();
        }
        return view('Admin/supplier/shuAdd');
    }
    public function shuAddPost(){
        $req=request();
        $re=DB::table('jtkj_goods_shus')->insert(['s_size'=>$req->size,'s_weight'=>$req->weight,'s_colour'=>$req->colour,'goods_id'=>$req->id,'s'=>$req->s,'w'=>$req->w]);
        if($re){
           
            return redirect()->action('Admin\SupplierController@shu');
        }else{
          echo "失败";
        }

    }

    public function shuDel($id){

      $del= DB::table('jtkj_goods_shus')->where('s_id',$id)->delete();
        if($del){
          return redirect('admin/shu');
        }else{
          return "删除失败";
        }
    }
}
